package flex.cc.vehicle_svr.util;

/**
 * 上游师傅坐标接口参数加密解密工具
 * @author dupenghui
 * @date 20211215
 */
public final class UpPointsApiHEX {
    public UpPointsApiHEX() {
    }

    public static byte[] hexString2Bytes(String src) {
        int l = src.length() / 2;
        byte[] ret = new byte[l];

        for(int i = 0; i < l; ++i) {
            ret[i] = Integer.valueOf(src.substring(i * 2, i * 2 + 2), 16).byteValue();
        }

        return ret;
    }

    public static String bytes2HexString(byte[] b) {
        StringBuffer result = new StringBuffer();

        for(int i = 0; i < b.length; ++i) {
            String hex = Integer.toHexString(b[i] & 255);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }

            result.append(hex.toUpperCase());
        }

        return result.toString();
    }

    public static String string2HexString(String strPart) {
        StringBuffer hexString = new StringBuffer();

        for(int i = 0; i < strPart.length(); ++i) {
            int ch = strPart.charAt(i);
            String strHex = Integer.toHexString(ch);
            hexString.append(strHex);
        }

        return hexString.toString();
    }

    public static String hexString2String(String src) {
        StringBuffer temp = new StringBuffer();

        for(int i = 0; i < src.length() / 2; ++i) {
            temp.append((char)Integer.valueOf(src.substring(i * 2, i * 2 + 2), 16).byteValue());
        }

        return temp.toString();
    }

    public static Byte char2Byte(Character src) {
        return Integer.valueOf(src).byteValue();
    }

    public static String intToHexString(int a, int len) {
        len <<= 1;
        String hexString = Integer.toHexString(a);
        int b = len - hexString.length();
        if (b > 0) {
            for(int i = 0; i < b; ++i) {
                hexString = "0" + hexString;
            }
        }

        return hexString;
    }
}